package leetcode.d17;

import org.junit.Test;

import java.util.*;

/**
 * 电话号码转换
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @author zhouxl
 * @DATE 2021/9/2
 * @descriptoin
 */
public class PhoneNumConvert {
    
    @Test
    public void letterCombinationsTest(){
        List<String> stringList = letterCombinations("23");
        System.out.println(stringList.toString());
    }

    public List<String> letterCombinations(String digits) {
        List<String> totalList = new ArrayList<>();
        if( digits == null || "".equals(digits)){
            return totalList;
        }
        List<List<String>> numString = getNumString(digits);
        totalList = permutationAndCombination(numString.get(0), numString);
        return totalList;
    }

    /**
     * 获取数字对应的字母数组
     * @return
     */
    public List<String> getNumListString(String num){
        Map<String,List<String>> numMap = new HashMap<>();
        numMap.put("2",Arrays.asList("a","b","c"));
        numMap.put("3",Arrays.asList("d","e","f"));
        numMap.put("4",Arrays.asList("g","h","i"));
        numMap.put("5",Arrays.asList("j","k","l"));
        numMap.put("6",Arrays.asList("m","n","o"));
        numMap.put("7",Arrays.asList("p","q","r","s"));
        numMap.put("8",Arrays.asList("t","u","v"));
        numMap.put("9",Arrays.asList("w","x","y","z"));
        return numMap.get(num);
    }

    public List<List<String>> getNumString(String digits){
        List<List<String>> strList = new ArrayList<>();
        for(int i=0 ; i<digits.length(); i++){
            strList.add(getNumListString(digits.charAt(i)+""));
        }
        return strList;
    }

    /**
     *  排列组合
     */
    public List<String> permutationAndCombination(List<String> totalList, List<List<String>> numList){
        for(int i=1; i<numList.size() ; i++){
            totalList = combinationString(totalList,numList.get(i));
        }
        return totalList;
    }

    public List<String> combinationString(List<String> totalList, List<String> currentList){
        List<String> list = new ArrayList<>();
        if( currentList == null || currentList.size() == 0 ){
            return totalList;
        }
        for(int i=0 ; i<totalList.size(); i++){
            String str = totalList.get(i);
            String temp = totalList.get(i);
            for(int j=0; j<currentList.size() ;j++){
                str += currentList.get(j);
                list.add(str);
                str = temp;
            }
        }
        return list;
    }


}
